package com.wlw.tools;

import com.wlw.util.util.DateUtil;

import org.apache.shiro.crypto.hash.SimpleHash;

import java.io.*;
import java.math.BigDecimal;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author wang
 * @since 2019-07-29 16:05
 */
public class Md5Util {

    /**
     * 密码加密-采用shiro方式
     *
     * @param text 密码原文
     * @return 加密串
     */
    public static String encrypt(String text) {
        //加密方式
        String hashAlgorithmName = "MD5";
        //盐值
        Object salt = "";
        //加密2次
        int hashIterations = 1;
        Object result = new SimpleHash(hashAlgorithmName, text, salt, hashIterations);
        return String.valueOf(result);
    }


   public static void main(String[]args) throws ParseException {
       //锦福1分钟一次,24小时跑，只需要当前时间减去开始时间就可以了
       Long a= DateUtil.formatStringToDate("00:00:00",DateUtil.DATE_FORMAT_HMS).getTime();//开始时间戳
       Long b=DateUtil.formatStringToDate("00:00:01",DateUtil.DATE_FORMAT_HMS).getTime();//当前时间戳
       Double allms=Double.valueOf(b-a)/1000;//总秒数
       Double qs1=allms/Integer.valueOf(60);//期数
       Double ms=60-allms%Integer.valueOf(60);//秒数
       int m= (int) ((Math.floor(qs1))+1);
       String qs=null;
       if(m<10){
           qs= DateUtil.formatDateToString(new Date(),DateUtil.DATE_FORMAT_YMD_New)+"000"+m;
       }else if(m>=10&&m<100){
           qs=DateUtil.formatDateToString(new Date(),DateUtil.DATE_FORMAT_YMD_New)+"00"+m;
       }else if(m>=100&&m<1000){
           qs=DateUtil.formatDateToString(new Date(),DateUtil.DATE_FORMAT_YMD_New)+"0"+m;
       }else{
           qs=DateUtil.formatDateToString(new Date(),DateUtil.DATE_FORMAT_YMD_New)+m;
       }
      System.out.println("锦福1粉彩期数---"+m+"--"+ms);
       System.out.println(DateUtil.formatDateToString(DateUtil.formatStringToDate("00:01:00",DateUtil.DATE_FORMAT_HMS),DateUtil.DATE_FORMAT_HMS));
   }
public static byte[] createChecksum(String filename) throws Exception {
    InputStream fis =  new FileInputStream(filename);          //将流类型字符串转换为String类型字符串

    byte[] buffer = new byte[1024];
    MessageDigest complete = MessageDigest.getInstance("MD5"); //如果想使用SHA-1或SHA-256，则传入SHA-1,SHA-256
    int numRead;

    do {
        numRead = fis.read(buffer);    //从文件读到buffer，最多装满buffer
        if (numRead > 0) {
            complete.update(buffer, 0, numRead);  //用读到的字节进行MD5的计算，第二个参数是偏移量
        }
    } while (numRead != -1);

    fis.close();
    return complete.digest();
}

    public static String getMD5Checksum(String filename) throws Exception {
        byte[] b = createChecksum(filename);
        String result = "";

        for (int i=0; i < b.length; i++) {
            result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring(1);//加0x100是因为有的b[i]的十六进制只有1位
        }
        return result;
    }


}
